using System;
using System.Data;
using TimeSheetDAL;
using System.Collections;

namespace TimeSheetBLL
{

	[TableAttribute("users")]
	[PrimaryKeyAttribute("user_id", "ID")]
	[ColumnAttribute("user_name", "UserName")]
	[ColumnAttribute("password", "Password")]
	[ColumnAttribute("email", "Email")]
	[ColumnAttribute("first_name", "FirstName")]
	[ColumnAttribute("last_name", "LastName")]
	[ColumnAttribute("date_created", "DateCreated")]
	[ColumnAttribute("active", "Active")] 
	public class User : IDBEntity
	{

		private string userName;
		private string password;
		private string email;
		private string firstName;
		private string lastName;
		private DateTime dateCreated;
		private bool active;
		private long iD;

		public User()
		{
			this.active = true;
		}

		public bool Authenticate()
		{
			DBFacade dbf = new DBFacade();
			return dbf.Authenticate( this );
		}

		public DataTable GetRecentProjects()
		{
			DBFacade dbf = new DBFacade();
			return dbf.GetRecentProjects( this.iD );
		}

		public UserProjects GetProjectsByPayPeriod( DateTime date )
		{
			DBFacade dbf = new DBFacade();
			return dbf.GetProjectsByPayPeriod( date, this.iD );
		}

		public DataTable GetNonChargeableHoursByPayPeriod( DateTime date )
		{
			DBFacade dbf = new DBFacade();
			return dbf.GetUserNonChargeableHoursByPayPeriod( date, this.iD );
		}

		public DataTable GetChargeableHoursByPayPeriod( DateTime date )
		{
			DBFacade dbf = new DBFacade();
			return dbf.GetUserChargeableHoursByPayPeriod( date, this.iD );
		}

		public Hashtable GetUserHoursByPayPeriod( DateTime date )
		{
			DBFacade dbf = new DBFacade();
			return dbf.GetUserHoursByDateRange( Utils.GetPayPeriodStart( date ), Utils.GetPayPeriodEnd( date ), this.iD );
		}

		public double GetTotalChargeableHours( DateTime start, DateTime end )
		{
			DBFacade dbf = new DBFacade();
			return dbf.GetTotalChargeableHours( this.iD, start, end );
		}

		public double GetTotalNonChargeableHours( DateTime start, DateTime end )
		{
			DBFacade dbf = new DBFacade();
			return dbf.GetTotalNonChargeableHours( this.iD, start, end );
		}

		public DataTable GetProjectHours( DateTime start, DateTime end )
		{
			DBFacade dbf = new DBFacade();
			return dbf.GetProjectHoursByStaff( this.iD, start, end );
		}

		public DataTable GetActivityHours( DateTime start, DateTime end )
		{
			DBFacade dbf = new DBFacade();
			return dbf.GetActivityHoursByStaff( this.iD, start, end );
		}

		public DataTable GetOvertimeHours( DateTime start, DateTime end, double hoursInDay )
		{
			DataTable ot = new DataTable();
			ot.Columns.Add( "claimed_date", typeof(System.DateTime) );
			ot.Columns.Add( "claimed_hours", typeof(System.Double) );

			DBFacade dbf = new DBFacade();
			Hashtable hours = dbf.GetUserHoursByDateRange( start, end, this.iD );
			foreach( DateTime date in hours.Keys )
			{
				if( date.DayOfWeek == System.DayOfWeek.Saturday || date.DayOfWeek == System.DayOfWeek.Sunday )
				{
					object [] otHours = new Object[2];
					otHours[0] = date;
					otHours[1] = ((DailyHours)hours[ date ]).GetTotalChargeableHours() + ((DailyHours)hours[ date ]).GetTotalNonChargeableHours();
					ot.Rows.Add( otHours );
				}
				else
				{
					double variance = ((DailyHours)hours[ date ]).GetVariance();
					if( variance > 0 )
					{
						object [] otHours = new Object[2];
						otHours[0] = date;
						otHours[1] = variance;
						ot.Rows.Add( otHours );
					}
				}
			}

			return ot;
		}


		#region Generated

		public long ID
		{
			get
			{
				return this.iD;
			}set
			 {
				 this.iD = value; 
			 }
		}

		public string FullName
		{
			get{ return this.firstName + " " + this.lastName; }
		}

		public string UserName
		{

			get
			{
				return this.userName;
			}set
			 {
				 this.userName = value; 
			 }
		}

		public string Password
		{

			get
			{
				return this.password;
			}set
			 {
				 this.password = value; 
			 }
		}

		public string Email
		{

			get
			{
				return this.email;
			}set
			 {
				 this.email = value; 
			 }
		}

		public string FirstName
		{

			get
			{
				return this.firstName;
			}set
			 {
				 this.firstName = value; 
			 }
		}

		public string LastName
		{

			get
			{
				return this.lastName;
			}set
			 {
				 this.lastName = value; 
			 }
		}

		public DateTime DateCreated
		{

			get
			{
				return this.dateCreated;
			}set
			 {
				 this.dateCreated = value; 
			 }
		}

		public bool Active
		{

			get
			{
				return this.active;
			}set
			 {
				 this.active = value; 
			 }
		}

		#endregion

	}//class
}//namespace